<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>角色</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<blockquote class="layui-elem-quote list_search">
        <div class="layui-inline">
            <a class="layui-btn layui-btn-normal roleAdd_btn"><i
                    class="layui-icon">&#xe608;</i> 添加权限</a>
        </div>
        <div class="layui-inline">
            <a class="layui-btn layui-btn-danger batchDel"
               data-type="delCheckData"><i class="layui-icon">&#xe640;</i>批量删除</a>
        </div>
</blockquote>
<table id="permList" class="permList" lay-filter="permList"></table>
<script type="text/html" id="barEdit">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script th:src="@{/src/layui.js}"></script>
<script>
    layui.use(['jquery','layer','laypage','laytpl','table'],function () {
        var $ = layui.jquery,
            layer = layui.layer,
            laypage = layui.laypage,
            laytpl = layui.laytpl,
            table = layui.table;
        table.render({
            elem:'#permList',
            url:'/perm/list',
            title:'权限列表',
            page:true,
            limit:10,
            limits:[10,20],
            cols:[
                [
                    {type:'checkbox'},
                    {title:'ID',field:'id',width:50},
                    {title:'权限标识',field:'permission'},
                    {title:'权限名称',field:'title'},
                    {title:'创建人',field:'adminName'},
                    {title:'创建时间',field:'createTime'},
                    {title:'更新时间',field:'updateTime'},
                    {title:'操作',templet:'#barEdit'}
                ]
            ],
            parseData:function(result){
                return {
                    code:result.code==200?0:result.code,
                    msg:result.message,
                    count:result.data.total,
                    data:result.data.list
                }
            }
        });

        //批量删除
        $('.batchDel').click(function () {
            //获取选中的数据
            var checkStatus = table.checkStatus('permList');
            var data = checkStatus.data;
            if (data.length == 0){
                layer.msg("请选择权限数据",{icon:5});
                return ;
            }
            var ids = '';
            $.each(data,function (index , perm) {//遍历数组，取出权限id
                if (index == data.length - 1){
                    ids += perm.id;
                } else {
                    ids += perm.id + ',';
                }
            })
            $.ajax({
                url:'/perm/batchDel',
                type:'delete',
                data:{'ids':ids},//传id字符串到后端
                dataType:'json',
                success : function (result) {
                    if (result.code == 200){
                        layer.msg(result.message,{icon:1},function () {
                            location.reload();
                        })
                    }else {
                        layer.msg(result.message,{icon:5});
                    }
                }
            })

        })
        //添加权限
        $(".roleAdd_btn").click(function () {
            var index = layer.open({
                type:2,
                title:'添加权限',
                content:'/system/addPerm'
            });
            //窗口大小发生改变的时候触发该事件
            $(window).resize(function () {
                layer.full(index);
            });
            layer.full(index);
        });
        //监听表格的行工具事件
        table.on('tool(permList)',function (row) {
            var data = row.data;
            if(row.event === 'edit'){
                layer.open({
                    type:2,
                    title:'编辑权限',
                    area:['400px','600px'],
                    content:'/perm/editPerm?id='+data.id
                });

            }else if(row.event === 'del'){
                layer.confirm("确定删除吗？",function (index) {
                    $.ajax({
                        url:'/perm/del',
                        type:'delete',
                        data:{'id':data.id},
                        dataType:'json',
                        success:function (result) {
                            if (result.code == 200){
                                layer.msg(result.message,{icon:1},function () {
                                    location.reload();//删除成功刷新页面
                                });
                            } else {
                                layer.msg(result.message,{icon:5});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        })




    })
</script>

</body>
</html>